var m;

layui.config({
    dir: '/eshop/static/layui/'
})
layui.use('table', function () {
    var $ = layui.$//通过layui获取到jquery
    var table = layui.table,form = layui.form, layer = layui.layer;
    //渲染数据表格
    table1 = table.render({
        elem: '#demo'
        , height: 450
        , url: '/eshop/carouselFigure.do?m=findAll' //数据接口
        , page: false //开启分页
        ,request: {
            pageName: 'current' //页码的参数名称，默认：page
            ,limitName: 'limit' //每页数据量的参数名，默认：limit
        }
        , cols: [[ //表头
            {field: 'id', title: 'ID', fixed: 'left'}
            , {field: 'url', title: '路径'}
            , {field: 'sequenceNum', title: '序号'}
            , {
                field: 'id', title: '操作', templet: function (d) {
                    console.log(d);
                    console.log(d.id);
                    return '<button str="'+d.id+'" onclick="update(this)" class="layui-btn layui-btn-sm">修改</button>' +
                        '<button str="'+d.id+'" onclick="deleteData(this)" class="layui-btn layui-btn-sm">删除</button>'
                }
            }
        ]]
    });

    //添加点击方法
    $("#add").click(function () {
        m='add';
        $("#downBox").empty();
        layer.open({
            type: 1,
            title: "添加轮播图",
            maxmin: false,
            shadeClose: true,
            area: ['400px', '350px'],
            content: $("#model")
            , success: function (layero) {
                //把内容放到遮罩层里
                var mask = $(".layui-layer-shade");
                mask.appendTo(layero.parent());
            }
        });
    });

    //提交添加和修改方法
    form.on('submit(formDemo)', function(obj){
        $.ajax({
            url:'/eshop/carouselFigure.do?m='+m,
            type:'post',
            data:obj.field,
            dataType:'json',
            success:function (data){
                layer.msg(JSON.stringify(data))
                if (data=='成功'){
                    layer.closeAll('page');
                    table1.reload('demo', {});
                }
            }
        })
        return false;
    });
});

//删除方法
function deleteData(d) {
    layer.confirm('您确定要删除这条数据吗？', {
        btn: ['确定','取消'] //按钮
    }, function()
    {
        layer.closeAll('dialog');
        $.ajax({
            type: 'get',
            url: '/eshop/carouselFigure.do?m=delete',
            data: {id: $(d).attr('str')},
            success: function (data) {
                layer.msg(JSON.stringify(data))
                if (data == '删除成功') {
                    layer.closeAll('page');
                    table1.reload('demo', {});
                }
            }
        })
    });

    // layer.closeAll('dialog');  //点击确定关闭提示框

}

//修改方法
function update(e) {
    m='update'
    $("#downBox").append('<label class="layui-form-label">序号</label>\n' +
        '            <div class="layui-input-inline">\n' +
        '                <input type="text" name="sequenceNum" required id="sequenceNum" lay-verify="required" placeholder="请输入序号" autocomplete="off" class="layui-input">\n' +
        '            </div>');
    $.ajax({
        url:"/eshop/carouselFigure.do?m=findByID",
        type:'get',
        data:{id:$(e).attr('str')},
        dataType: "json",
        success:function (data) {
            console.log(data);

            $("#id").val(data.id)
            $("#img").attr("src","/eshop/Img.do?m=showImg&name="+data.url);
            $("#imgUrl").val(data.url)
            $("#sequenceNum").attr("max",data.maxNum)
            $("#sequenceNum").val(data.maxNum)
        }
    })
    layer.open({
        type: 1,
        title: "修改轮播图",
        maxmin: false,
        shadeClose: true,
        area: ['400px', '350px'],
        content: $("#model")
        , success: function (layero) {
            //把内容放到遮罩层里
            var mask = $(".layui-layer-shade");
            mask.appendTo(layero.parent());
        }
    });
}
//图片上传方法
layui.use('upload', function(){
    var upload = layui.upload;

    //执行实例
    var uploadInst = upload.render({
        elem: '#file' //绑定元素
        ,url: '/eshop/Img.do?m=uploadImg' //上传接口
        ,done: function(res){
            //上传完毕回调
            $("#img").attr("src","/eshop/Img.do?m=showImg&name="+res.data.src);
            $("#imgUrl").val(res.data.src)
        }
        ,error: function(){
            //请求异常回调
            console.log("error")
        }
    });
});